package zr.dao;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Currency;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import zr.Main;

public class QiPan extends JPanel {
	public static int qipan[][][] = {
			{ { 19, 17 }, { 109, 17 }, { 203, 17 }, { 297, 17 }, { 387, 17 } },
			{ { 19, 59 }, { 109, 59 }, { 203, 59 }, { 297, 59 }, { 387, 59 } },
			{ { 19, 100 }, { 109, 100 }, { 203, 100 }, { 297, 100 },
					{ 387, 100 } },
			{ { 19, 143 }, { 109, 143 }, { 203, 143 }, { 297, 143 },
					{ 387, 143 } },
			{ { 19, 186 }, { 109, 186 }, { 203, 186 }, { 297, 186 },
					{ 387, 186 } },
			{ { 19, 230 }, { 109, 230 }, { 203, 230 }, { 297, 230 },
					{ 387, 230 } },
			{ { 19, 358 }, { 109, 358 }, { 203, 358 }, { 297, 358 },
					{ 387, 358 } },
			{ { 19, 400 }, { 109, 400 }, { 203, 400 }, { 297, 400 },
					{ 387, 400 } },
			{ { 19, 445 }, { 109, 445 }, { 203, 445 }, { 297, 445 },
					{ 387, 445 } },
			{ { 19, 489 }, { 109, 489 }, { 203, 489 }, { 297, 489 },
					{ 387, 489 } },
			{ { 19, 526 }, { 109, 526 }, { 203, 526 }, { 297, 526 },
					{ 387, 526 } },
			{ { 19, 566 }, { 109, 566 }, { 203, 566 }, { 297, 566 },
					{ 387, 566 } } };

	public int safeHouse[][] = { { 2, 1 }, { 2, 3 }, { 3, 2 }, { 4, 1 },
			{ 4, 3 }, { 7, 1 }, { 7, 3 }, { 8, 2 }, { 9, 1 }, { 9, 3 } };

	private ImageIcon imageicon;

	private int width, height;

	public QiPan() {
		super();
		imageicon = new ImageIcon("QiPan.jpg");
		width = imageicon.getIconWidth();
		height = imageicon.getIconHeight();
		this.setSize(width, height);
		this.setLayout(null);

		this.addMouseListener(new MouseListener() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (Main.myTurn == true) {
					int x = e.getX();
					int y = e.getY();
					Point point = getNearestPoint(x, y);
					
					QiZi qz=QiZi.get(point.x, point.y);//看看所获取到的Point处是否有棋子（一般应该是没有的）
					if(qz!=null)//如果有的话，就做点击棋子的操作
						qz.button.doClick();
					
					point=QiPan.get(point);
					
					if(Main.currentSelectQizi!=null){//说明用户想把当前选中的移动到point处
						if(Operation.in(point,Operation.oneStepArea(Main.currentSelectQizi))){//如果此点可达
							Main.write(Operation.走子 + ":["
									+ Main.currentSelectQizi.point.x
									+ ","
									+ Main.currentSelectQizi.point.y
									+ "][" + point.x + ","
									+ point.y + "]");
							Main.currentSelectQizi.moveTo(point);							
						}
					}
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
			}

			@Override
			public void mouseExited(MouseEvent e) {
			}

			@Override
			public void mousePressed(MouseEvent e) {
			}

			@Override
			public void mouseReleased(MouseEvent e) {
			}
		});
	}

	protected static Point get(Point point) {
		Point p=new Point();
		for(int i=0;i<qipan.length;i++){
			for(int j=0;j<qipan[0].length;j++)
				if(qipan[i][j][0]==point.x&&qipan[i][j][1]==point.y){				
					p.setLocation(i, j);
					break;
				}				
		}
		return p;
	}

	protected Point getNearestPoint(int x, int y) {
		Point point = null;
		double minDistance = Integer.MAX_VALUE;
		int row = 0, col = 0;
		for (row = 0; row < qipan.length; row++)
			for (col = 0; col < qipan[0].length; col++) {
				double xx = qipan[row][col][0] + 15;
				double yy = qipan[row][col][1] + 5;
				double distance = Math.sqrt((xx - x) * (xx - x) + (yy - y)
						* (yy - y));
				if (distance < QiZi.WIDTH && distance < minDistance) {
					point = new Point();
					point.setLocation(qipan[row][col][0], qipan[row][col][1]);
					minDistance = distance;
				}
			}
		return point;
	}

	public boolean isSafeHouse(int row, int col) {
		boolean bool = false;
		for (int i = 0; i < safeHouse.length; i++) {
			if (safeHouse[i][0] == row && safeHouse[i][1] == col) {
				bool = true;
				break;
			}
		}

		return bool;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		imageicon.paintIcon(this, g, 0, 0);
	}

	public Dimension getPreferredSize() {
		return new Dimension(imageicon.getIconWidth(), imageicon
				.getIconHeight());
	}

	public void randomLayoutQiZi(List<QiZi> list) {
		int max = list.size();
		for (int i = 0; i < 100; i++) {
			int index1 = (int) (Math.random() * max);
			int index2 = (int) (Math.random() * max);
			QiZi qz1 = list.get(index1);
			QiZi qz2 = list.get(index2);
			list.set(index1, qz2);
			list.set(index2, qz1);
		}
		int row = 0, col = 0;
		for (int i = 0; i < list.size(); i++) {
			QiZi qz = list.get(i);
			if (isSafeHouse(row, col) == false) {
				this.add(qz.button);
				qz.button.setLocation(qipan[row][col][0], qipan[row][col][1]);
				qz.point.x=row;
				qz.point.y=col;
				
			} else
				i--;
			col++;
			if (col >= qipan[0].length) {
				col = 0;
				row++;
			}
		}
	}
}
